package com.zhoutian.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.zhoutian.constant.MessageConstant;
import com.zhoutian.entity.PageResult;
import com.zhoutian.entity.QueryPageBean;
import com.zhoutian.entity.Result;
import com.zhoutian.pojo.Role;
import com.zhoutian.service.RoleService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/role")
public class RoleController {

    @Reference//查找服务
    private RoleService roleService;

    //新增角色
    @PreAuthorize("hasAuthority('RLOE_ADD')")//权限校验
    @RequestMapping("/add")
    public Result add(@RequestBody Role role, Integer[] permissionIds){
        try{
            roleService.add(role,permissionIds);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_RLOE_FAIL);//新增角色失败
        }
        return new Result(true,MessageConstant.ADD_RLOE_SUCCESS);//新增角色成功
    }

    //删除角色
    @PreAuthorize("hasAuthority('RLOE_DELETE')")//权限校验
    @RequestMapping("/delete")
    public Result delete(Integer id){
        try{
            roleService.deleteById(id);
        }catch (Exception e){
            e.printStackTrace();
            //服务调用失败
            return new Result(false, MessageConstant.DELETE_RLOE_FAIL);//删除角色失败
        }
        return  new Result(true, MessageConstant.DELETE_RLOE_SUCCESS);//删除角色成功
    }

    //分页查询
    @PreAuthorize("hasAuthority('RLOE_QUERY')")//权限校验
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        return roleService.pageQuery(queryPageBean);
    }

    //编辑角色
    @PreAuthorize("hasAuthority('RLOE_EDIT')")//权限校验
    @RequestMapping("/edit")
    public Result edit(@RequestBody Role role, Integer[] permissionIds){
        try{
            roleService.edit(role,permissionIds);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_RLOE_FAIL);//编辑角色失败
        }
        return new Result(true,MessageConstant.EDIT_RLOE_SUCCESS);//编辑角色成功
    }

    //根据ID查询角色
    @PreAuthorize("hasAuthority('RLOE_QUERY')")//权限校验
    @RequestMapping("/findById")
    public Result findById(Integer id){
        try{
            Role role = roleService.findById(id);
            return new Result(true,MessageConstant.QUERY_RLOE_SUCCESS,role);//查询角色成功
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_RLOE_FAIL);//查询角色失败
        }
    }

    //根据角色ID查询关联的权限ID
    @PreAuthorize("hasAuthority('RLOE_QUERY')")//权限校验
    @RequestMapping("/findPermissionIdsByRoleId")
    public Result findPermissionIdsByRoleId(Integer id) {
        try{
            List<Integer> permissionIds = roleService.findPermissionIdsByRoleId(id);
            return new Result(true,MessageConstant.QUERY_RLOE_SUCCESS,permissionIds);//查询成功
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_RLOE_FAIL);//查询失败
        }
    }

    //查找全部角色信息
    @RequestMapping("/findAll")
    public Result findAll(){
        try{
            List<Role> list = roleService.findAll();
            return  new Result(true, MessageConstant.QUERY_RLOE_SUCCESS,list);//查询角色成功
        }catch (Exception e){
            e.printStackTrace();
            //服务调用失败
            return new Result(false, MessageConstant.QUERY_RLOE_FAIL);//查询角色失败
        }
    }

}
